/*
 * Graphics.java
 *
 * Created on October 11, 2009, 1:08 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.nextel.util.lwuit;

/**
 * Abstracts the underlying platform graphics context thus allowing us to achieve
 * portability between MIDP devices and CDC devices. This abstaction simplifies
 * and unifies the Graphics implementations of various platforms.
 * 
 * <p>A graphics instance is never created by the developer and is always accessed
 * using either a paint callback or a mutable image. There is no way to create this
 * object directly.
 */
public final class Graphics {
    private int xTranslate;
    private int yTranslate;
    private int color;
    private Font current ;

//    private LWUITImplementation impl;
    private javax.microedition.lcdui.Graphics nativeGraphics;
    
    
    /**
     * Constructing new graphics with a given javax.microedition.lcdui.Graphics 
     * @param g a given javax.microedition.lcdui.Graphics
     */
    public Graphics(javax.microedition.lcdui.Graphics nativeGraphics) {
        setGraphics(nativeGraphics);
//        impl = Display.getInstance().getImplementation();
    }

    /**
     * Setting graphics with a given javax.microedition.lcdui.Graphics
     * 
     * @param g a given javax.microedition.lcdui.Graphics
     */
    void setGraphics(javax.microedition.lcdui.Graphics g) {
        this.nativeGraphics = g;
    }

    /**
     * Returns javax.microedition.lcdui.Graphics
     * 
     * @return a javax.microedition.lcdui.Graphics object
     */
    public Object getGraphics() {
        return nativeGraphics;
    }

    /**
     * Returns the current color
     * 
     * @return the RGB graphics color 
     */
    public int getColor() {
        return color;
    }

    /**
     * Sets the current rgb color while ignoring any potential alpha component within
     * said color value.
     * 
     * @param RGB the RGB value for the color.
     */
    public void setColor(int RGB) {
        color = 0xffffff & RGB;
        nativeGraphics.setColor(color);
        //impl.setColor(nativeGraphics, color);
    }

    /**
     * Returns the font used with the drawString method calls 
     * 
     * @return the font used with the drawString method calls
     */
    public Font getFont() {
        return current;
    }

    /**
     * Sets the font to use with the drawString method calls 
     * 
     * @param font the font used with the drawString method calls
     */
    public void setFont(Font font) {
        this.current = font;
//        if(font instanceof SystemFont) {
//            nativeGraphics.setFont((javax.microedition.lcdui.Font)((SystemFont)font).getNativeFont());
//            //impl.setNativeFont(nativeGraphics, );
//        }
    }

    /**
     * Returns the x clipping position
     * 
     * @return the x clipping position
     */
    public int getClipX() {
        return nativeGraphics.getClipX() - xTranslate;
        //return impl.getClipX(nativeGraphics) - xTranslate;
    }

    /**
     * Returns the y clipping position
     * 
     * @return the y clipping position
     */
    public int getClipY() {
        return nativeGraphics.getClipY() - yTranslate;
        //return impl.getClipY(nativeGraphics) - yTranslate;
    }

    /**
     * Returns the clip width
     * 
     * @return the clip width
     */
    public int getClipWidth() {
        //return impl.getClipWidth(nativeGraphics);
        return nativeGraphics.getClipWidth();
    }

    /**
     * Returns the clip height
     * 
     * @return the clip height
     */
    public int getClipHeight() {
        //return impl.getClipHeight(nativeGraphics);
        return nativeGraphics.getClipHeight();
    }

    /**
     * Clips the given rectangle by intersecting with the current clipping region, this
     * method can thus only shrink the clipping region and never increase it.
     * 
     * @param x the x coordinate of the rectangle to intersect the clip with
     * @param y the y coordinate of the rectangle to intersect the clip with
     * @param width the width of the rectangle to intersect the clip with
     * @param height the height of the rectangle to intersect the clip with
     */
    public void clipRect(int x, int y, int width, int height) {
        //impl.clipRect(nativeGraphics, xTranslate + x, yTranslate + y, width, height);
        nativeGraphics.clipRect(xTranslate + x, yTranslate + y, width, height);
    }

    /**
     * Updates the clipping region to match the given region exactly
     * 
     * @param x the x coordinate of the new clip rectangle.
     * @param y the y coordinate of the new clip rectangle.
     * @param width the width of the new clip rectangle.
     * @param height the height of the new clip rectangle.
     */
    public void setClip(int x, int y, int width, int height) {
        nativeGraphics.setClip(xTranslate + x, yTranslate + y, width, height);
    }

    /**
     * Draws a line between the 2 X/Y coordinates
     * 
     * @param x1 first x position
     * @param y1 first y position
     * @param x2 second x position
     * @param y2 second y position
     */
    public void drawLine(int x1, int y1, int x2, int y2) {
        nativeGraphics.drawLine(xTranslate + x1, yTranslate + y1, xTranslate + x2, yTranslate + y2);
    }
    
    public void drawArc(int x1, int y1, int x2, int y2, int star, int arco) {
        nativeGraphics.drawArc(xTranslate + x1, yTranslate + y1, xTranslate + x2, yTranslate + y2, star, arco);
    }
//
    /**
     * Fills the rectangle from the given position according to the width/height
     * minus 1 pixel according to the convention in Java.
     * 
     * @param x the x coordinate of the rectangle to be filled.
     * @param y the y coordinate of the rectangle to be filled.
     * @param width the width of the rectangle to be filled.
     * @param height the height of the rectangle to be filled.
     */
    public void fillRect(int x, int y, int width, int height) {
        nativeGraphics.fillRect(xTranslate + x, yTranslate + y, width, height);
    }
    
    public void fillArc(int x, int y, int width, int height, int star, int arco) {
        nativeGraphics.fillArc(xTranslate + x, yTranslate + y, width, height, star, arco);
    }

    /**
     * Draws a rectangle in the given coordinates
     * 
     * @param x the x coordinate of the rectangle to be drawn.
     * @param y the y coordinate of the rectangle to be drawn.
     * @param width the width of the rectangle to be drawn.
     * @param height the height of the rectangle to be drawn.
     */
    public void drawRect(int x, int y, int width, int height) {
        nativeGraphics.drawRect( xTranslate + x, yTranslate + y, width, height);
    }



    /**
     * Draw a string using the current font and color in the x,y coordinates. The font is drawn
     * from the top position and not the baseline.
     * 
     * @param str the string to be drawn.
     * @param x the x coordinate.
     * @param y the y coordinate.
     */
    public void drawString(String str, int x, int y) {

//        if(current instanceof SystemFont) {
//            System.out.println("***dibujando con un SystemFont");
//            nativeGraphics.drawString(str, x + xTranslate, y + yTranslate,javax.microedition.lcdui.Graphics.TOP | javax.microedition.lcdui.Graphics.LEFT);
//        } else {
//            System.out.println("***dibujando con un CustomFont");
            char[] data = str.toCharArray();
            current.drawChars(this, data, 0, data.length, x, y);
//        }
    }

    public void drawString(String str, int x, int y, int anchor) {
//        System.out.println("empieza el dibujado 4.4");
//        if(current instanceof SystemFont) {
////            System.out.println("***dibujando con un SystemFont");
//            nativeGraphics.drawString(str, x + xTranslate, y + yTranslate,anchor);
//        } else {
//            System.out.println("***dibujando con un CustomFont");
            char[] data = str.toCharArray();
            current.drawChars(this, data, 0, data.length, x, y);
//        }
    }
        
    /**
     * Draw the given char using the current font and color in the x,y 
     * coordinates. The font is drawn from the top position and not the 
     * baseline.
     * 
     * @param character - the character to be drawn
     * @param x the x coordinate of the baseline of the text
     * @param y the y coordinate of the baseline of the text
     */
    public void drawChar(char character, int x, int y) {
        drawString("" + character, x, y);
    }

    /**
     * Draw the given char using the current font and color in the x,y 
     * coordinates. The font is drawn from the top position and not the 
     * baseline.
     * 
     * @param character - the character to be drawn
     * @param x the x coordinate of the baseline of the text
     * @param y the y coordinate of the baseline of the text
     */
    public void drawChar(char character, int x, int y, int anchor) {
        drawString("" + character, x, y, anchor);
    }
    
    /**
     * Draw the given char array using the current font and color in the x,y coordinates. The font is drawn
     * from the top position and not the baseline.
     * 
     * @param data the array of characters to be drawn
     * @param offset the start offset in the data
     * @param length the number of characters to be drawn
     * @param x the x coordinate of the baseline of the text
     * @param y the y coordinate of the baseline of the text
     */
    public void drawChars(char[] data, int offset, int length, int x, int y) {
//        if(current instanceof SystemFont) {
//            drawString(new String(data, offset, length), x, y);
//        } else {
            CustomFont f = (CustomFont)current;
            f.drawChars(this, data, offset, length, x, y);
//        }
    }

    /**
     * Draws the image so its top left coordinate corresponds to x/y
     * 
     * @param img the specified image to be drawn. This method does 
     * nothing if img is null.
     * @param x the x coordinate.
     * @param y the y coordinate.
     */
    public void drawImage(Image img, int x, int y) {
        img.drawImage(this, nativeGraphics, x, y);
    }

    public void drawImage(javax.microedition.lcdui.Image img, int x, int y) {
        nativeGraphics.drawImage(img, x + xTranslate, y + yTranslate, javax.microedition.lcdui.Graphics.TOP | javax.microedition.lcdui.Graphics.LEFT);
    }

    public void drawImage(javax.microedition.lcdui.Image img, int x, int y, int anchor) {
        nativeGraphics.drawImage(img, x + xTranslate, y + yTranslate, anchor);
    }

}

